package com.zznj.xzjc.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.vondear.rxtool.view.RxToast;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.AreaAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.AreaBean;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.FormBody;


/**
 * Created by JY on 2017/11/15.
 */

public class SelectAreaActivity1 extends BaseActivity {
    @BindView(R.id.rec_main)
    RecyclerView recMain;

    @BindView(R.id.selectTv)
    TextView selectTv;
    @BindView(R.id.sureBtn)
    TextView sureBtn;
    @BindView(R.id.resetBtn)
    TextView resetBtn;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_select_area_layout;
    }


    private List<AreaBean> mCityList;
    private List<AreaBean> mAreaList;
    private List<AreaBean> mList;
    private AreaAdapter mAdapter;
    private int type = 0; //得到城市；1--得到县
    private AreaBean city;
    private AreaBean area;
    private AreaAdapter areaAdapter;

    private String queryType;
    @Override
    public void initView() {
        setTitle("区划选择");
        queryType = getIntent().getExtras().getString("queryType");
        init();
    }


    private int cityPosition=-1;
    private void init() {
        mCityList = new ArrayList<>();
        mAreaList = new ArrayList<>();
        mList = new ArrayList<>();
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        //防止Item切换
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recMain.setLayoutManager(layoutManager);

        mAdapter = new AreaAdapter(R.layout.gyss_item_area, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AreaBean bean = mList.get(position);
                if (type == 0) {
                    area = null;
                    cityPosition = position;
                    city = mCityList.get(cityPosition);
                    if("horAndUp".equals(queryType)){
                        selectTv.setText(bean.getName());
                        mAdapter.setSelectPosition(cityPosition);
                        return;
                    }else{

                    }
                } else {
                    if (position == 0) {
                        type = 0;
                        areaCode = "";
                        mList.clear();
                        mList.addAll(mCityList);
                        mAdapter.setType(type);
                        mAdapter.setSelectPosition(cityPosition);
                        selectTv.setText(mCityList.get(cityPosition).getName());
                    } else {
                        area = bean;
                        mAdapter.setSelectPosition(position);
                        if(mCityList.get(cityPosition).getName().equals(area.getName())){
                            selectTv.setText(area.getName());
                        }else{
                            selectTv.setText(mCityList.get(cityPosition).getName() + ">" + area.getName());
                        }
                    }
                }
            }
        });
    }

    private LoginBean user;
    private String selfLevel = "false";
    private String areaCode;
    private String areaName;
    private String upCode;


    @Override
    public void initData() {
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        selectTv.setText("");
        areaCode = "";
        getArea();
    }

    private void getArea() {
        SubscriberOnNextListener<BaseBean<List<AreaBean>>> onNextListener = baseBean -> {
            mList.clear();
            mAdapter.setType(type);
            if (type == 0) {
                mList.addAll(baseBean.getResult());
                mAdapter.setSelectPosition(cityPosition);
                mCityList.clear();
                mCityList.addAll(baseBean.getResult());
            } else {
                AreaBean bean = new AreaBean();
                bean.setName("返回");
                mList.add(bean);
                mList.addAll(baseBean.getResult());
                mAdapter.setSelectPosition(-1);
                mAdapter.notifyDataSetChanged();
                mAreaList.clear();
                mAreaList.addAll(baseBean.getResult());
            }
        };
        FormBody requestBody = new FormBody.Builder()
                .add("queryType", queryType)
                .add("jurisDiction",areaCode)
                .build();
        for (int i = 0; i < requestBody.size(); i++) {
            System.out.println("XXX请求参数：" + requestBody.name(i) + ":" + requestBody.value(i));
        }
        HttpMethods.getInstance().getDistrictTreeData(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    @OnClick({R.id.resetBtn,R.id.sureBtn})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.resetBtn){
            type = 0;
            selectTv.setText("");
            areaCode = "";
            cityPosition = -1;
            city = null;
            area = null;
            getArea();
        }else if(view.getId() == R.id.sureBtn){
            if(area != null){
                areaCode = area.getCode();
                areaName = area.getName();
                upCode = area.getUpCode();
                selfLevel = area.getSelfLevel();
            }else if (city != null){
                areaCode = city.getCode();
                areaName = city.getName();
                upCode = city.getUpCode();
                selfLevel = city.getSelfLevel();
            }else{
                RxToast.normal("请先选择区划");
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("areaCode",areaCode);
            intent.putExtra("areaName",areaName);
            intent.putExtra("upCode",upCode);
            intent.putExtra("selfLevel",selfLevel);
            setResult(RESULT_OK,intent);
            finish();
        }
    }
}
